define(window.backboneCommon.getViewDefine("finance.index"), function ($,require,Collection,tpl,ViewAgentTransfer,ViewAgentStream,ViewCustomerTransfer,ViewCustomerConsume) {
    var backboneView= Backbone.View.extend({
        events: {
            "shown.bs.tab #tab_group a[data-toggle='tab']": "changeTabs"
        },
        initialize: function () {
            this.listenTo(this.model, 'bind:SurplusAmount', this.bindSurplusAmount);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(){
            this.elementModel={};
            this.extObject={};
            this.pageData={isRefreshAgentStream:true};
            this.elementModel.tab=$("#tab_group");
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("list",["agent","advertiser"],function(json){
                self.pageData.powerInfo=json;
                self.pageData.power={
                    agent:json["agent"],
                    advertiser:json["advertiser"]
                };
                //self.pageData.power.agent=false;
                self._initBodyMain();
            });
        },
        _initBodyMain:function(){
            this.initViewAttach();
            this.collection.getSurplusAmount();
            var tabType=this.elementModel.tab.children().first().children("a").data("tabPageType");
            this._selectTabs(tabType);
        },
        initViewAttach:function () {
            var self=this;
            self.viewAttach={};
            if(self.pageData.power.agent){
                self.viewAttach.agentTransfer=new ViewAgentTransfer({pElement:"#tab_agentTransfer",
                    getSurplusAmountCallback:function () {
                        return self.pageData.surplusAmount;
                    },
                    transferCallback:function () {
                        self.pageData.isRefreshAgentStream=true;
                        self.collection.getSurplusAmount();
                    }
                });
                self.viewAttach.agentStream=new ViewAgentStream({pElement:"#tab_agentStream"});
            }else if(self.pageData.power.advertiser){
                self.viewAttach.customerTransfer=new ViewCustomerTransfer({pElement:"#tab_customerTransfer"});
                self.viewAttach.customerConsume=new ViewCustomerConsume({pElement:"#tab_customerConsume"});
            }
        },
        bindSurplusAmount:function () {
            var res=this.model.get("dataSurplusAmount");
            if(res.success){
                this.pageData.surplusAmount=res.data.surplusAmount;
                $("#span_surplusAmount").text(this.pageData.surplusAmount).ExtNumeric("refresh");
            }
        },
        _selectTabs:function (type) {
            var elTab=this.elementModel.tab.find('[data-tab-page-type="'+type+'"]');
            elTab.parent().addClass("active").siblings().removeClass("active");
            var element= $("#tab_"+type);
            element.addClass("active").siblings().removeClass("active");
            this._operateTabs(elTab);
        },
        changeTabs:function (e) {
            this._operateTabs($(e.currentTarget))
        },
        _operateTabs:function (element) {
            var type = element.data("tabPageType");
            var text = element.find("span").text();
            $(".page-operate-title").text(text);
            switch(type){
                case "agentStream":
                    this.viewAttach.agentStream.init(this.pageData.isRefreshAgentStream);
                    this.pageData.isRefreshAgentStream=false;
                    break;
                default:
                    this.viewAttach[type].init();
                    break;
            }

        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({ el: "#container_body",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function () {
            this.view.initMain();
        }
    };
    return viewModel;
});